package com.hzz.web.boss.sys;

import com.hzz.api.sys.ISysVariableService;
import com.hzz.dto.sys.SysVariableDto;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.ALogOperation;
import com.hzz.framework.annotation.RfUser2Bean;
import com.hzz.framework.annotation.auth.Logical;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>系统_数据字典  ACTION类。
 */
@RestController
@Slf4j
public class SysVariableController extends BaseController {
    private static final String acPrefix = "/boss/sys/variable/";
    @Autowired
    protected ISysVariableService variableService;

    /**
     * <p> 信息树json。
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "tree")
    @ResponseBody
    public Response tree() {
        log.info("tree.........");
        Response result = new Response(0, "success");
        try {
            result.data = variableService.findDataIsTree(null);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p> 信息分页 (未删除)。
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "page/{pageNum}")
    public Response page(@ModelAttribute SysVariableDto dto, @PathVariable("pageNum") Integer pageNum) {
        log.info("page.........");
        Response result = new Response(0, "success");
        try {
            dto.setPageNum(pageNum);
            dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
            result.data = getPageDto(variableService.findDataIsPage(dto));
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p> 编辑。
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info/{id}")
    public Response info(@PathVariable("id") Long id) {
        log.info("info.........");
        Response result = new Response(0, "success");
        try {
            SysVariableDto dto = new SysVariableDto();
            dto.setId(id);
            result.data = variableService.findDataById(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <li>删除。
     */
    @RequiresPermissions("sysVariable:del")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "del/{id}")
    @ALogOperation(type = "删除", desc = "数据字典信息")
    public Response del(@PathVariable("id") Long id) {
        log.info("del.........");
        Response result = new Response(0, "success");
        try {
            SysVariableDto dto = new SysVariableDto();
            dto.setId(id);
            result.message = variableService.deleteData(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p> 信息保存
     */
    @RequiresPermissions(value = {"sysVariable:add", "sysVariable:edit"}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "save")
    @RfUser2Bean
    @ALogOperation(type = "修改", desc = "数据字典信息")
    public Response save(@Validated SysVariableDto dto, BindingResult bindingResult) {
        log.info("save.........");
        Response result = new Response(0, "success");
        try {
            if (bindingResult.hasErrors()) {
                String errorMsg = "";
                List<ObjectError> errorList = bindingResult.getAllErrors();
                for (ObjectError error : errorList) {
                    errorMsg += (error.getDefaultMessage()) + ";";
                }
                result = Response.error(errorMsg);
            } else {
                result = variableService.saveOrUpdateData(dto);
                result.data = dto.getId();
            }
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }
}